<template>
  <div>
    <el-card>
      <div slot="header" class='clearfix'>
        <Breadcrumb>添加文章</Breadcrumb>
      </div>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <!-- <el-form-item label="内容">
          <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item> -->
    <!-- 富文本编辑器内容 -->
        <el-form-item label="内容" prop="content">
         <quill-editor
          v-model="form.content"
          :options="editorOption"></quill-editor>
        </el-form-item>
        <el-form-item label="封面">
          <el-radio-group v-model="form.coverType" @change="hReSetImageList">
            <el-radio :label="1" name="type">单图</el-radio>
            <el-radio :label="3" name="type">三图</el-radio>
            <el-radio :label="0" name="type">无图</el-radio>
            <el-radio :label="-1" name="type">自动</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="" v-if="form.coverType>0">
          <el-row>
            <el-col :span="6" v-for="(item,idx) in form.coverType" :key="idx">
                {{idx}}
                <Cover v-model="form.coverImages[idx]"/>
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 频道 -->
        <Channel v-model="form.channelId" />
        <el-form-item>
          <el-button type="primary" @click="hAdd(false)">发表</el-button>
          <el-button @click="hAdd(true)">存入草稿</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 导入样式
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
// 导入组件
import { quillEditor } from 'vue-quill-editor'
import { getArticles } from '../../../../VUE/课程资料/hmpc/src/api/article'

export default {
  components: {
    quillEditor
  },
  name:'AddArticle',
  data () {
    return{
      //对富文本编辑器配置
      editorOption: {
      // 占位配置
        placeholder: '',
        modules: {
          // 配置工具栏
          toolbar: [
              ['bold', 'italic', 'underline', 'strike'],
              ['blockquote', 'code-block'],
              [{ header: 1 }, { header: 2 }],
              [{ list: 'ordered' }, { list: 'bullet' }],
              [{ indent: '-1' }, { indent: '+1' }],
              ['image']
          ]
        }
      },
      form:{
        // 默认设置，传入数据可修改
        title:'',
        content:'',
        coverType:0, 
        ChannelId:'',
        coverImages: []   //保存对应的封面图地址
      },
      //表单验证规则：
      //  格式：  键：要验证的字段，值是一个数组，每一项是一条规则 
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' },
          { min: 5, max: 30, message: '长度在 5 到 30 个字符', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ],
        channelId: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }, 
    }
  },
  methods:{
    hReSetImageList () {
      if (this.form.coverType === 3) {
      } else if (this.form.coverType === 1) {
        this.form.coverImages = [this.form.coverImages[0]]
      }
    },
    async hAdd (isDraft) {
      // 用户点击保存草稿
      // 1.收集数据
      console.log(this.form);
      // 2.验证数据   el-form
      this.$refs.form.validata(result => {
        console.log(result)
        //验证通过
        if (result) {
          this.doAdd(isDraft)
        }
      })
    },
    async doAdd (isDraft) {
      // 3.调用接口
      try{
      const result = await this.$http({
        method:'POST',
        url:'mp/v1_0/articles?draft=' + isDraft,
        data:{
          // 对应文章标题
          title:this.form.title,
          // 对应文章内容
          content:this.form.content,
          // 对应封面
          cover:{
            type:this.form.coverType,
            images:[]   //this.form.coverImages
          },
          // 对应所属频道ID
          channel_Id:this.form.ChannelId
        }
      })
      console.log(result);
      // 提示成功
      this.$message.success('恭喜成功！')
    } catch (err) {
      console.log(err);
      this.$message.error('操作失败！')
      }
    },
      // 4.根据结果反馈
    onSubmit () {
      console.log(this.form);
    }
  }
}

</script>

<style scoped lang='less'>
  // 在父组件中去设置子组件的样式，加上/deep/ 实现穿透功能
  .publish-container /deep/ .ql-editor{
    height: 300px;
  }
  .publish-container /deep/ .ql-toolbar.ql-snow{
    padding: 0 8px;
  }
</style>